# serializers.py
from rest_framework import serializers
from .models import Candidate


# 1. 候选人列表序列化器（精简字段）
class CandidateSerializer(serializers.ModelSerializer):
    # 显示choices中文标签（如“待面试”而非0）
    first_interview_result_display = serializers.ReadOnlyField(source='get_first_interview_result_display')
    final_status_display = serializers.ReadOnlyField(source='get_final_status_display')
    # 显示面试官姓名（而非id）
    first_interviewer_name = serializers.ReadOnlyField(source='first_interviewer.name', allow_null=True)

    class Meta:
        model = Candidate
        fields = [
            'id', 'name', 'gender', 'age', 'apply_position',
            'first_interview_result', 'first_interview_result_display',
            'first_interviewer_name', 'final_status', 'final_status_display',
            'create_time',
        ]


# 2. 候选人详情序列化器（完整字段）
class CandidateDetailSerializer(serializers.ModelSerializer):
    # 所有choices的中文标签
    gender_display = serializers.ReadOnlyField(source='get_gender_display')
    degree_display = serializers.ReadOnlyField(source='get_degree_display')
    first_interview_result_display = serializers.ReadOnlyField(source='get_first_interview_result_display')
    second_interview_result_display = serializers.ReadOnlyField(source='get_second_interview_result_display')
    third_interview_result_display = serializers.ReadOnlyField(source='get_third_interview_result_display')
    final_status_display = serializers.ReadOnlyField(source='get_final_status_display')
    # 所有面试官姓名
    first_interviewer_name = serializers.ReadOnlyField(source='first_interviewer.name', allow_null=True)
    second_interviewer_name = serializers.ReadOnlyField(source='second_interviewer.name', allow_null=True)
    third_interviewer_name = serializers.ReadOnlyField(source='third_interviewer.name', allow_null=True)

    class Meta:
        model = Candidate
        fields = '__all__'


class InterviewAssignSerializer(serializers.Serializer):
    round = serializers.ChoiceField(choices=['first', 'second', 'third']) # 限制传参
    interviewer_id = serializers.IntegerField() # 限制传参 只能是int类型
    interview_date = serializers.DateField()  # 限制传参   只能是日期类型  YY  MM DD

    def vaildate(self, data):
        from staff.models import Employee
        try:
            interviewer = Employee.objects.get(id=data['interviewer_id'], status='在职')
        except Employee.DoesNotExist:
            raise serializers.ValidationError("未找到的面试官interviewer_id")
        return data
